//
//  LoginViewController.m
//
//  Created by David Hinson on 10/9/08.
//  Copyright 2009 Cliqset, Inc.. All rights reserved.
//

#import "LoginViewController.h"

@implementation LoginViewController

@synthesize myNavBar;
@synthesize navigationController;
@synthesize f1, f2;

NSString *k2loginKey		= @"loginKey";
NSString *k2passwordKey		= @"passwordKey";

// Override initWithNibName:bundle: to load the view using a nib file then perform additional customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}

 
- (IBAction)cancelView:(id)sender{
	//NSLog(@"Cancel clicked...");
}

-(void)insertLogoArtIntoTitle {
	
	UIImage* graphic		  = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"banner" ofType:@"png"]];
	UIImageView *showImage    = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 43.0)];
    showImage.opaque          = NO;    
	showImage.image           = graphic;
	
	[myNavBar addSubview:showImage];
	[graphic release];
	[showImage release];
}

// Implement viewDidLoad to do additional setup after loading the view.
- (void)viewDidLoad {
	
	f1 = [[[UITextField alloc] initWithFrame:CGRectMake(110, 10, 100, 20)] retain];
	f2 = [[[UITextField alloc] initWithFrame:CGRectMake(110, 10, 100, 20)] retain];

	[self insertLogoArtIntoTitle];
	
    [super viewDidLoad];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    // return (interfaceOrientation == UIInterfaceOrientationPortrait);
	[super shouldAutorotateToInterfaceOrientation:interfaceOrientation];
	return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	tableView.backgroundColor = [UIColor clearColor];
	return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
	return nil;
}

-(void)textFieldLoginDidChange:(id)textField {	
	NSMutableString* strLogin = (NSMutableString*)[[textField object] text];
	NSUserDefaults *standardUserDefaults = (NSUserDefaults *)[NSUserDefaults standardUserDefaults];
	[standardUserDefaults setObject:strLogin forKey:k2loginKey];
}

-(void)textFieldPasswordDidChange:(id)textField {	
	NSMutableString* strPassword = (NSMutableString*)[[textField object] text];
	NSUserDefaults *standardUserDefaults = (NSUserDefaults *)[NSUserDefaults standardUserDefaults];
	[standardUserDefaults setObject:strPassword forKey:k2passwordKey];
} 

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
	[textField resignFirstResponder];
	
	NSUserDefaults *standardUserDefaults = (NSUserDefaults *)[NSUserDefaults standardUserDefaults];
	
	if (textField.secureTextEntry) {
		// Set password
		[standardUserDefaults setObject:textField.text forKey:k2passwordKey];
	} else {
		// Set login
		[standardUserDefaults setObject:textField.text forKey:k2loginKey];
	}
	
	return YES;
}	

// Table row has been processed...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	//NSLog(@"Clicked");
	if (indexPath.row == 0) {
		[f1 becomeFirstResponder];
	}
	if (indexPath.row == 1) {
		[f2 becomeFirstResponder];
	}
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
	
	UILabel* userIDLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 80, 20)];
	UILabel* passWordLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 80, 20)];

    UIColor* myBlue = [UIColor colorWithRed:0.0/255.0 green:0.0/255.0 blue:132.0/255.0 alpha:0.75];
	
	[f1 setPlaceholder:@"Required"];
	[f2 setPlaceholder:@"Required"];
	[f1 setTextColor:myBlue];
	[f2 setTextColor:myBlue];
	[f2 setSecureTextEntry:YES];
	
	f1.text = [[NSUserDefaults standardUserDefaults] stringForKey:k2loginKey];
	f2.text = [[NSUserDefaults standardUserDefaults] stringForKey:k2passwordKey];
	
	[f1 setReturnKeyType:1];
	[f2 setReturnKeyType:1];
	
	[userIDLabel setText:@"Username"];
	[passWordLabel setText:@"Password"];
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }
	
	cell.selectionStyle = UITableViewCellSelectionStyleNone;
	
	[f1 setDelegate:self];
	[f2 setDelegate:self];
	
	f1.autocapitalizationType = UITextAutocapitalizationTypeNone;
	f2.autocapitalizationType = UITextAutocapitalizationTypeNone;
	
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldLoginDidChange:) name:@"UITextFieldTextDidChangeNotification" object:f1];
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldPasswordDidChange:) name:@"UITextFieldTextDidChangeNotification" object:f2];
	
	switch (indexPath.row) {
		case 0:
			[cell addSubview:userIDLabel];
			[cell addSubview:f1];			
			break;
		case 1:
			[cell addSubview:passWordLabel];
			[cell addSubview:f2];
			break;
	}
	
	[userIDLabel release];
	[passWordLabel release];
    
    // Set up the cell
    return cell;
}

- (void)dealloc {	
	[myNavBar release];
	[f1 release];
	[f2 release];	
	[navigationController release];	
    [super dealloc];
}


@end
